import { storage, router, request } from '@doctorwork/saga';

export default {
    namespace: 'auth_h5',
    state: {
        redirect: '', //鉴权跳转地址
        phone: '',
        valiCode: '',
        disableGetCode: false, //等待获取验证码网络状态
        canGetCode: false,
        timeRemain: 61,
        showBindMode: true,
        loaddingFlat: false,
        needBindPhone: false, //是否需要绑定手机号
        router: {} //路由参数
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        // 静默登录
        *loginSilent({}, { put, select }) {
            const params = yield select(state => state.auth_h5.router);
            const { token, openId } = params;
            // 1. 保存token
            yield put.resolve(storage.set('authToken', token));
            // 2. 获取用户信息
            const userInfo = yield put.resolve({
                type: '$request/get',
                payload: {
                    url: '/sso/passport/profile'
                }
            });

            console.log('userInfo', userInfo);
            // 3. 验证是否有手机号
            if (userInfo.phone) {
                yield put.resolve(
                    storage.set('userInfo', Object.assign(userInfo, { openId }))
                );
                const redirect = yield put.resolve(storage.get('redirect'));
                if (redirect) {
                    yield put.resolve(
                        router.redirect({
                            url: redirect
                        })
                    );
                }
            } else {
                yield put({
                    type: 'save',
                    payload: {
                        needBindPhone: true
                    }
                });
            }
        },
        // 获取验证码
        *getValidateCode({ payload }, { put }) {
            yield put({
                type: '$request/get',
                payload: {
                    url: '/sso/passport/smscode',
                    data: payload
                }
            });
            yield put({
                type: 'save',
                payload: {
                    canGetCode: true,
                    timeRemain: 60
                }
            });
        },
        // 手动输入手机号绑定账号
        *login({ payload }, { put }) {
            // 1. 登录SSO，保存token
            const data = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/sso/passport/login',
                    data: payload
                }
            });
            yield put.resolve(storage.set('authToken', data.token));

            // 2. 登录业务方，获取userInfo
            const userInfo = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/api/user/login'
                }
            });

            // // 3. 获取用户信息
            const profile = yield put(
                request.get({
                    url: '/api/user'
                })
            );
            userInfo.openId = profile.openId;
            // 开始存储
            if (userInfo.phone) {
                userInfo.userId = userInfo.userIdStr;
                userInfo.openId = data.openId;
                yield put.resolve(storage.set('userInfo', userInfo));
                yield put({
                    type: '$msg/showToast',
                    payload: {
                        title: '登录成功'
                    }
                });
                yield put(router.back());
            }
        },
        // 手动输入手机号绑定账号
        *bindPhone({ payload }, { put }) {
            // 1. 登录SSO，保存token
            const result = yield put.resolve({
                type: '$request/get',
                payload: {
                    url: '/sso/passport/bind_phone_smart',
                    data: {
                        phone: payload.account,
                        verifyCode: payload.verifyCode
                    }
                }
            });
            if (result) {
                yield put({ type: 'loginSilent' });
            }
        }
    }
};
